package work.chief.servlet;

import work.chief.entity.Category;
import work.chief.entity.Product;
import work.chief.entity.User;
import work.chief.service.CategoryService;
import work.chief.service.ProductService;
import work.chief.service.TrolleyService;
import work.chief.service.impl.CategoryServiceImpl;
import work.chief.service.impl.ProductServiceImpl;
import work.chief.service.impl.TrolleyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "indexServlet",urlPatterns = "/indexServlet")
public class IndexServlet extends BaseServlet {
    //实例化对象
    private CategoryService categoryService = new CategoryServiceImpl();
    private ProductService productService = new ProductServiceImpl();
    private TrolleyService trolleyService = new TrolleyServiceImpl();

    public void showInfoIndex(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Category> categoryList= categoryService.selectCategory(10);    //查询10条
        //将数据存入到作用域中
        request.setAttribute("categoryList",categoryList);

        //查询state=4单品信息
        List<Product> productListMing=productService.selectByState(3);
        //将获取到的数据库存到作用域中
        request.setAttribute("productListMing",productListMing);

        //查询家电信息
        List<Product> productListCName=productService.selectByCName("家电");
        request.setAttribute("productListCName",productListCName);
//        System.out.println(productListCName);

        //根据uid查询购物车总数
        User user = (User)request.getSession().getAttribute("user");
        Integer count = trolleyService.selectTrolleyCount(user.getId());
//        request.setAttribute("count",count);
        //存入session
        request.getSession().setAttribute("count",count);

        //转发到界面
        request.getRequestDispatcher("index.jsp").forward(request,response);
    }
}
